package library.gui.scene;

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;

import library.core.user.Reader;
import library.core.user.User;
import library.gui.SystemApplication;
import library.gui.component.BookView;
import library.gui.component.FunctionalButton;
import library.gui.component.InputList;
import library.gui.component.UserPane;
import library.gui.component.VButtonList;
import library.gui.dialog.NotiDialog;
import library.gui.dialog.PostDialog;
import library.utils.Info;

public class OperatePane extends BorderPane{
	
	SystemApplication handler;
	
	private final int leftWidth = 140;
	
	private BorderPane left;
	private BookView bookView;
	
	private UserDescPane userDescPane;
	
	public void refreshUser(User user) {
		UserPane userPane = new UserPane(user, leftWidth);
		left.setTop(userPane);
		userDescPane = new UserDescPane(user);
		userDescPane.prefWidthProperty().set(leftWidth);
		left.setCenter(userDescPane);
	}
	
	static class UserDescPane extends VBox {
		
		public UserDescPane(User user) {
			this.setAlignment(Pos.TOP_CENTER);
			this.setStyle("-fx-border-style: solid; -fx-border-color: silver");
			addDesc("邮箱", user.getEmail());
			addDesc(user.descDepartment(), user.getDepartment());
			if(user.isReader()) {
				addDesc("权限", "读者");
				addDesc("状态", ((Reader)user).isFrozen() ? "冻结" : "正常");
			}
			else {
				addDesc("权限", "管理员");
			}
		}
		
		public void addDesc(String key, String value) {
			Label title = new Label(key), v = new Label(value);
			title.prefWidthProperty().bind(this.prefWidthProperty());
			title.setStyle("-fx-background-color: black;");
			title.setTextFill(Color.WHITE);
			v.setAlignment(Pos.CENTER);
			v.prefWidthProperty().bind(this.widthProperty());
			this.getChildren().addAll(title, v);
		}
		
	}
	
	public OperatePane(SystemApplication handler) {

		{
			left = new BorderPane();
			User user = handler.getCurrentUser();
			
			refreshUser(user);
			
			VButtonList buttonList = new VButtonList(
				new FunctionalButton("修改密码", ()->{
					InputList inputList = new InputList()
						.addSecretedKey("原密码").addSecretedKey("新密码");
					new PostDialog("修改密码", inputList, 
							new FunctionalButton("取消", ()->{}),
							new FunctionalButton("确定", ()->{
								Info info = user.modifyPassword(inputList.get("原密码"), inputList.get("新密码"));
								if(info.isSuccess())
									new NotiDialog("修改成功！", info.toString()).showAndWait();
								else 
									new NotiDialog("修改失败！", info.toString()).showAndWait();
							})).showAndWait();
				}),
				new FunctionalButton("修改资料", ()->{
					InputList inputList = new InputList()
							.addKey("用户名").addKey("邮箱").addChoiceKey(user.descDepartment(), SystemApplication.DEPARTMENT)
							.setDefaults(user.getName(), user.getEmail(), user.getDepartment());
					new PostDialog("修改资料", inputList, 
							new FunctionalButton("取消", ()->{}),
							new FunctionalButton("确定", ()->{
								Info info = user.modifyInfo(
									inputList.get("用户名"), inputList.get("邮箱"), inputList.get(user.descDepartment()));
								if(info.isSuccess()) {
									new NotiDialog("修改成功！", info.toString()).showAndWait();
									refreshUser(user);
								}
								else 
									new NotiDialog("修改失败！", info.toString()).showAndWait();
							})).showAndWait();
				}),
				new FunctionalButton("退出", handler::toMainMenuScene));
			buttonList.setPrefWidth(leftWidth);
			buttonList.setPrefButtonHeight(30);
			
			left.setPadding(new Insets(5));
			left.setBottom(buttonList);
		}

		bookView = new BookView(handler);
		
//		this.setPadding(new Insets(10));
		
		this.setLeft(left);
		this.setCenter(bookView);
		
	}
	
}
